package cn.petplatform.petback.controller.petpublish;

import cn.petplatform.annotations.LoginVerifyToken;
import cn.petplatform.annotations.PassToken;
import cn.petplatform.dto.ResponseDto;
import cn.petplatform.petback.pojo.Pet;
import cn.petplatform.petback.service.pettypes.PetTypesService;
import cn.petplatform.petback.service.publishpets.PublishPetsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


/**
 * 宠物发布情况
 *
 * @author LNL
 * @date 2021/5/15 15:55
 */
@RestController
@Api(tags = "宠物发布模块")
///petPublish/publishPets
@RequestMapping("/petPublish")
public class PetPublishController {

    @Autowired
    private PublishPetsService publishPetsService;

    @Autowired
    private PetTypesService petTypesService;

    @PassToken
    @ApiOperation("获取最新发布的宠物信息")
    @GetMapping("/publishPets")
    public ResponseDto getAllPublishPets() {
        //控制器调用业务层的逻辑
        ResponseDto responseDto = publishPetsService.getAllPublishPets();

        return responseDto;
    }

    @PassToken
    @ApiOperation("获取所有发布的宠物信息")
    @GetMapping("/publishes")
    public ResponseDto getAllPublishes() {
        //获取所有发布的宠物信息
        ResponseDto responseDto = publishPetsService.getAllPublishes();
        return responseDto;
    }

    @PassToken
    @ApiOperation("通过用户id获取到用户对应的宠物信息")
    @GetMapping("/publishPets/{id}")
    public ResponseDto getPublishPetsByPerson(@PathVariable("id") String id) {
        ResponseDto responseDto = publishPetsService.getPublishPetsByPerson(id);
        return responseDto;
    }

    @PassToken
    @ApiOperation("通过发布id获取宠物信息")
    @GetMapping("/detail/{id}")
    public ResponseDto getDetailPet(@PathVariable("id") String id) {
        ResponseDto responseDto = publishPetsService.getDetailPet(id);
        return responseDto;
    }


    /**
     * 获取所有的宠物类型
     *
     * @return
     */
    @PassToken
    @ApiOperation("获取所有的宠物类型")
    @GetMapping("/petTypes")
    public ResponseDto getPetTypes() {
        ResponseDto responseDto = petTypesService.getPetTypes();
        return responseDto;
    }


    /**
     * 上传宠物操作,需要验证token
     *
     * @return
     */
    @LoginVerifyToken
    @PostMapping("/insert")
    @ApiOperation("上传新的宠物")
    public ResponseDto insertPublishPet(MultipartHttpServletRequest request) {
        ResponseDto responseDto = publishPetsService.insertPublishPet(request);
        return responseDto;
    }

    @LoginVerifyToken
    @PostMapping("/delete/{id}")
    @ApiOperation("删除发布的宠物")
    public ResponseDto deletePublishPet(@ApiParam("发布条目id") @PathVariable("id") String id) {
        ResponseDto responseDto = publishPetsService.deletePublishPet(id);
        return responseDto;
    }

    @GetMapping("/publishesSearch/{msg}")
    @ApiOperation(value = "通过条件筛选")
    @PassToken
    public ResponseDto getAdoptPets(@PathVariable("msg") String msg) {
        ResponseDto responseDto = publishPetsService.getPublishPets(msg);
        return responseDto;
    }


}
